﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Concurrent;
using System.Configuration;

namespace YmtAuth.Common
{
    public class ConfigUtility
    {
        private static int queryLoginSafeStatusDayConfig = -1;

        /// <summary>
        /// 过滤前X天的风控记录
        /// </summary>
        public static int QueryLoginSafeStatusDayConfig
        {
            get
            {
                if (queryLoginSafeStatusDayConfig == -1)
                {
                    string queryDay = ConfigurationManager.AppSettings["QueryLoginSafeStatusDayConfig"];

                    queryLoginSafeStatusDayConfig = queryDay.ConvertToInt32(1);
                }

                return queryLoginSafeStatusDayConfig;
            }

        }

        /// <summary>
        /// TOKEN认证增加tokensource参数,是否完全开启（对所有token认证类请求，tokensource是必选参数）
        /// </summary>
        public static bool EnableTokensource
        {
            get
            {
                return string.Equals(ConfigurationManager.AppSettings["EnableTokensource"], "1", StringComparison.InvariantCultureIgnoreCase);
            }
        }

        /// <summary>
        /// TOKEN认证分流到JAVA的比例, 1表示10%， 10表示完全分流，0表示不分流
        /// </summary>
        public static int JAVASpreadRate
        {
            get
            {
                return ConfigurationManager.AppSettings["JAVASpreadRate"].ConvertToInt32(0);
            }
        }

        /// <summary>
        /// 获取用户凭据分流到JAVA的比例, 1表示10%， 10表示完全分流，0表示不分流
        /// </summary>
        public static int SpreadUserBillsRate
        {
            get
            {
                return ConfigurationManager.AppSettings["SpreadUserBillsRate"].ConvertToInt32(0);
            }
        }

        /// <summary>
        /// 登录风控调用Java版的登录风控服务，按比例分流，1表示10%， 10表示完全分流，0表示不分流
        /// </summary>
        public static int SpreadJavaLoginAuth
        {
            get
            {
                return ConfigurationManager.AppSettings["SpreadJavaLoginAuth"].ConvertToInt32(0);
            }
        }

        /// <summary>
        /// 用户安全兼容配置，客户端10/31号上线后更新此配置；安全问题->双重验证
        /// </summary>
        public static bool UseDualSecurity
        {
            get
            {
                if (ConfigurationManager.AppSettings["UseDualSecurity"] == null) return true;

                return string.Equals(ConfigurationManager.AppSettings["UseDualSecurity"], "1");
            }

        }

        /// <summary>
        /// 安全问题：用户校验安全问题超过最大次数后，需要等待多少天才能重新验证
        /// </summary>
        public static int NextSecurityQuestionVerifyDayCount
        {
            get
            {
                var _NextSecurityQuestionVerifyDayCount = System.Configuration.ConfigurationManager.AppSettings["NextSecurityQuestionVerifyDayCount"];
                return _NextSecurityQuestionVerifyDayCount.ConvertToInt32(2);
            }
        }

        /// <summary>
        /// 登录风控表, 配置SQL超时时间
        /// </summary>
        public static int LoginSafeSQLCommandTimeOut
        {
            get
            {
                var loginSafeSQLCommandTimeOut = System.Configuration.ConfigurationManager.AppSettings["LoginSafeSQLCommandTimeOut"];
                return loginSafeSQLCommandTimeOut.ConvertToInt32(10);
            }
        }
    }
}
